//
//  DR_BaseVC.swift
//  DocReaderRP
//
//  Created by  mac on 2025/11/10.
//

import UIKit

class DR_BaseVC: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .white
        // 隐藏导航栏（整个导航栏会消失）
        navigationController?.navigationBar.isHidden = true
    }

}

extension UIViewController {
    /// Push a view controller and hide the tab bar on the pushed screen.
    /// - Parameters:
    ///   - viewController: destination controller
    ///   - animated: whether to animate the push
    func pushHidingTabBar(_ viewController: UIViewController, animated: Bool = true) {
        viewController.hidesBottomBarWhenPushed = true
        if let nav = self.navigationController {
            nav.pushViewController(viewController, animated: animated)
        } else if let nav = self as? UINavigationController {
            nav.pushViewController(viewController, animated: animated)
        }
    }
}

extension UIViewController {
   
    
    class func topViewController() -> UIViewController? {
        guard let rootVc = UIApplication.shared.keyWindow?.rootViewController else { return nil }
        return getCurrentVcFrom(rootVc)
    }
      
    class private func getCurrentVcFrom(_ rootVc: UIViewController?) -> UIViewController? {
        guard let rootCtr = rootVc else { return nil }
          
        if let presentedVc = rootCtr.presentedViewController {
            return getCurrentVcFrom(presentedVc) // 递归调用
        }
          
        if rootCtr.isKind(of: UITabBarController.self) {
            if let selectedVc = (rootCtr as? UITabBarController)?.selectedViewController {
                return getCurrentVcFrom(selectedVc) // 递归调用
            }
        } else if rootCtr.isKind(of: UINavigationController.self) {
            if let visibleVc = (rootCtr as? UINavigationController)?.visibleViewController {
                return getCurrentVcFrom(visibleVc) // 递归调用
            }
        }
          
        return rootCtr
    }

}
